import jsPDF from 'jspdf'
import simfangNormal from '@/utils/simfang-normal'
import 'jspdf-autotable'
export default {
    install(Vue, options) {
        Vue.prototype.downPDF = function (dom, title) {
            const doc = new jsPDF('p', 'pt');
            doc.addFileToVFS('simfang-normal.ttf', simfangNormal);  // 引入字体，防止乱码
            doc.addFont('simfang-normal.ttf', 'simfang', 'normal'); // 添加字体
            doc.setFont('simfang')  // 设置字体
            doc.text(docX(doc, title), 25, title); // 设置表格标题
            doc.autoTable({
                html: `#${dom}`,
                theme: 'grid',
                styles: { font: "simfang", textColor: '#333', lineColor: '#666', cellWidth: 'wrap', valign: "middle", minCellHeight: 26, fontSize: 12, overflow: "linebreak" }
            });
            doc.save(`${title}.pdf`)
        }

        function docX(doc, title) {  // 计算头部标题
            const fontSize = doc.internal.getFontSize();
            const pageWidth = doc.internal.pageSize.width;
            const txtWidth = doc.getStringUnitWidth(title) * fontSize / doc.internal.scaleFactor;
            console.log((pageWidth - txtWidth) / 2);
            return (pageWidth - txtWidth) / 2;
        }
    }
}

